<?php
namespace app\user\controller;
use app\common\controller\Common;
use think\Request;
class Order extends Common
{
	protected $model = null;
	protected $keyword_search = ['order.order_sn','order.username','order.user_sn','order.phone','order.order_money','order.stay_money','goods.category_id','goods.cat_id','goods.brand_name','goods.memory_name','goods.attribute_name','goods.colour_name','goods.contract_sn'];
	protected $order = 'order.order_id desc';

	public function _initialize()
	{
		parent::_initialize();
        $this->model = model('Order');
	}
	/**
	 * 客户订单列表
	 */
	public function index()
	{
		if(Request()->isGet()){
			$data = Request()->param('');
			if(Request()->isAjax()){
				$page = isset($data['page']) ? $data['page'] : 1;
				$row = isset($data['limit']) ? $data['limit'] : $this->row;
				
				$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
				$search = $this->keyword_search($search_arr);
				$order = $this->order;
				$where = $this->handleList($data);
				// print_r($where);
				$list = $this->model->orderList($where, $order, $row, $search);
				$count = $this->model->orderCount($where, $order, $search);
				$consum = $this->model->consum($where, $order, $search);
				$this->success(['list'=>$list, 'count'=>$count, 'consum'=>$consum]);
			}
			/*@产品分类*/
			$cate = model('Category')->where('fid',0)->select();
			$this->assign('cate',$cate);
			return $this->fetch('');
		}
	}
	/**
	 * 客户订单详情
	 */
	public function details($ids = null)
	{
		$row = $this->model->get(['order_id'=>$ids]);
		$goods = $this->model->orderGoods($ids);
		$activity = $this->model->orderActivity($ids);
		if(Request()->isGet()){
			$this->assign('activity',$activity);
			$this->assign('goods',$goods);
			$this->assign('row',$row);
			return $this->fetch('');
		}
	}
	/**
	 * 列表查询条件处理
	 * @param array $data 查询参数
	 */
	protected function handleList($data)
	{
		$where = [];
		/*@已支付订单*/
		// $where['order.state'] = 1;
		/*@客户编号*/
		if(isset($data['user_sn']) && $data['user_sn'] != ''){
			$where['order.user_sn'] = $data['user_sn'];
		}
		/*@产品分类*/
		if(isset($data['category_id']) && $data['category_id'] != ''){
			$where['goods.category_id'] = $data['category_id'];
		}
		/*@产品子分类*/
		if(isset($data['cat_id']) && $data['cat_id'] != ''){
			$where['goods.cat_id'] = $data['cat_id'];
		}
		/*@产品品牌*/
		if(isset($data['brand_name']) && $data['brand_name'] != ''){
			$where['goods.brand_name'] = $data['brand_name'];
		}
		/*@产品内存*/
		if(isset($data['memory_name']) && $data['memory_name'] != ''){
			$where['goods.memory_name'] = $data['memory_name'];
		}
		/*@产品属性*/
		if(isset($data['attribute_name']) && $data['attribute_name'] != ''){
			$where['goods.attribute_name'] = $data['attribute_name'];
		}
		/*@产品颜色*/
		if(isset($data['colour_name']) && $data['colour_name'] != ''){
			$where['goods.colour_name'] = $data['colour_name'];
		}
		/*@时间区间*/
		if(isset($data['start']) && $data['start'] != '' && isset($data['end']) && $data['end'] != ''){
			if(strtotime($data['start']) > strtotime($data['end'])){
				return false;
			}
			$where['order.addtime'] = [
				['egt', strtotime($data['start'])],
				['lt', strtotime($data['end'])]
			];
		}else if(isset($data['start']) && $data['start'] != ''){
			$where['order.addtime'] = ['egt', strtotime($data['start'])];
		}else if(isset($data['end']) && $data['end'] != ''){
			$where['order.addtime'] = ['lt', strtotime($data['end'])];
		}
		return $where;
	}
}